Completed
Pull Request — develop (#224)
by Xaver
32s
created

link.js ➔ ... ➔ setData   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 6
rs 9.4285
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A link.js ➔ ... ➔ data.links.filter 0 3 1
1
define(['helper', 'snabbdom'], function (helper, V) {
2
  'use strict';
3
  V = V.default;
4
5
  function showStatImg(img, o, d, time) {
6
    var subst = {
7
      '{SOURCE_ID}': d.source.node_id,
8
      '{SOURCE_NAME}': d.source.hostname.replace(/[^a-z0-9\-]/ig, '_'),
9
      '{SOURCE_MAC}': d.source_mac,
10
      '{TARGET_ID}': d.target.node_id,
11
      '{TARGET_NAME}': d.target.hostname.replace(/[^a-z0-9\-]/ig, '_'),
12
      '{TARGET_MAC}': d.target_mac,
13
      '{TYPE}': d.type,
14
      '{TIME}': time,
15
      '{LOCALE}': _.locale()
16
    };
17
18
    img.push(V.h('h4', helper.listReplace(o.name, subst)));
19
    img.push(helper.showStat(V, o, subst));
20
  }
21
22
  return function (el, d, linkScale) {
23
    var self = this;
24
    var header = document.createElement('div');
25
    var table = document.createElement('table');
26
    var images = document.createElement('div');
27
    el.appendChild(header);
28
    el.appendChild(table);
29
    el.appendChild(images);
30
31
    self.render = function render() {
32
      var children = [];
33
      var img = [];
34
      var time = d[0].target.lastseen.format('DDMMYYYYHmmss');
35
36
      header = V.patch(header, V.h('div', V.h('h2', [
37
        V.h('a', {
38
          props: { href: router.generateLink({ node: d[0].source.node_id }) }
39
        }, d[0].source.hostname),
40
        V.h('span', ' - '),
41
        V.h('a', {
42
          props: { href: router.generateLink({ node: d[0].target.node_id }) }
43
        }, d[0].target.hostname)
44
      ])));
45
46
      helper.attributeEntry(V, children, 'node.hardware', (d[0].source.model ? d[0].source.model + ' – ' : '') +
47
        (d[0].target.model ? d[0].target.model : ''));
48
      helper.attributeEntry(V, children, 'node.distance', helper.showDistance(d[0]));
49
50
      d.forEach(function (link) {
51
        children.push(V.h('tr', { props: { className: 'header' } }, [
52
          V.h('th', _.t('node.connectionType')),
53
          V.h('th', link.type)
54
        ]));
55
        helper.attributeEntry(V, children, 'node.tq', V.h('span',
56
          { style: { color: linkScale((link.source_tq + link.target_tq) / 2) } },
57
          helper.showTq(link.source_tq) + ' - ' + helper.showTq(link.target_tq))
58
        );
59
60
        if (config.linkTypeInfos) {
61
          config.linkTypeInfos.forEach(function (o) {
62
            showStatImg(img, o, link, time);
63
          });
64
        }
65
      });
66
67
      if (config.linkInfos) {
68
        config.linkInfos.forEach(function (o) {
69
          showStatImg(img, o, d[0], time);
70
        });
71
      }
72
73
      var elNew = V.h('table', children);
74
      table = V.patch(table, elNew);
75
      table.elm.classList.add('attributes');
76
      images = V.patch(images, V.h('div', img));
77
    };
78
79
    self.setData = function setData(data) {
80
      d = data.links.filter(function (a) {
81
        return a.id === d[0].id;
82
      });
83
      self.render();
84
    };
85
    return self;
86
  };
87
});
88